quiet = "--quiet" %in% commandArgs(FALSE)
formats = commandArgs(TRUE)

src = (function() {
  attr(body(sys.function()), 'srcfile')
})()$filename
if (is.null(src) || src == '') src = '.'
owd = setwd(dirname(src))

# provide default formats if necessary
if (length(formats) == 0 || formats == "all") formats = c(
  'bookdown::pdf_book', 'bookdown::epub_book', 'bookdown::gitbook'
)
# render the book to all formats unless they are specified via command-line args

for (fmt in formats) {
  cmd = sprintf("bookdown::render_book('index.Rmd', '%s', quiet = %s)", fmt, quiet)
  res = bookdown:::Rscript(c('-e', shQuote(cmd)))
  if (res != 0) stop('Failed to compile the book to ', fmt)
}


if ("bookdown::gitbook" %in% formats) {
  if (.Platform$OS.type == "windows") {
    warning("zip commands may not avaliable on windows.")
  } else {
    if (file.exists("_book/cdacode.zip")) {
      file.remove("_book/cdacode.zip")
    }

    zip(
      "_book/cdacode.zip", "_book", 
      extras = "-x *.pdf *.tex *.epub *.mobi *.zip _book/CNAME"
    )
  }
}


